<?php


use Biz\model\brand\Content as mBrandContent;
use Biz\service\brand\SearchSchedule;

/**
 * @deprecated
 * Description: 多个品牌住友说
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/3/11
 * Time: 16:44
 */
class BrandContentListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    
    public function main()
    {
        $page = $this->getRequest()->getPost('page', 1);
        $per_page = $this->getRequest()->getPost('per_page', 20);
        $brand_ids = \Biz\model\brandActivity\BrandShowActivity::getAllNormalBrandIds("brand_id");
        if (!$brand_ids){
            throw new \Exception('未有品牌参加活动');
        }
        $ids = $this->_getInfo($brand_ids);
        $uid = $this->uid;
        //用户id
        $params['uid'] = $uid;
        //品牌id
        $params['page'] = $page;
        $params['per_page'] = $per_page;
        
        $params['ids'] = $ids;
        
        $response = \Biz\service\wiki\WikiContent::getlistByContentIds($params);
        
        
        $this->setSuccess($response);
        
    }
    
    /**
     * ES获取住友说数据存储缓存
     * @param $brand_ids
     * @return array|mixed
     */
    private function _getInfo($brand_ids): array
    {
        
        $key = \Biz\cache\activity\WikiContent::_CACHE_KEY_PREFIX_ . date("Y-m-d");
        $data = \Biz\cache\activity\WikiContent::getItem($key);
        
        if (isset($data) && $data) {
            return $data;
        }
        
        $data = [];
        foreach ($brand_ids as $brand_id) {
            //es搜索条件构造
            $esCondition = [
                'brand_id' => $brand_id,
                'page' => 1,
                'page_size' => 50,
                'is_owner' => false,
                'sort' => 'hot',
                'filter_audit_status' => [
                    mBrandContent::AUDIT_STATUS_REJECT,
                ],
                'claim_content_status' => mBrandContent::CLAIM_CONTENT_DONE,
                'is_top' => 1,
            ];
            //es搜索内容列表
            $info = SearchSchedule::search(new \Biz\module\Classes\adm\ContentSearch($esCondition));
            $ids = $info['list'] ? array_column($info['list'], 'id') : [];
            if ($ids) {
                $data = array_merge($ids, $info['list']);
            }
        }
        shuffle($data);
        if ($data) {
            $key = \Biz\cache\activity\WikiContent::_CACHE_KEY_PREFIX_ . date("Y-m-d");
            $ex = 24 * 3600;
            \Biz\cache\activity\WikiContent::setItem($key, $data, $ex);
        }
        return $data;
    }
}

